import util from '@/libs/util';
import Cookies from 'js-cookie';
export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                type: 'index'
            }, {
                title: '教室名称',
                key: 'name'
            }, {
                title: '所属区域',
                key: 'areaName'
            }, {
                title: '所在楼名',
                key: 'buildName'
            }, {
                title: '座位数量',
                key: 'setAmount'
            }, {
                title: '操作',
                key: 'action',
                width: 300,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    this.doDel(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            dataList: [],
            formDataRules: {// 添加班级表单验证规则
                name: [
                    { required: true, message: '请输入教室名称', trigger: 'blur' }
                ],
                areaId: [
                    { type: 'number', required: true, message: '请选择区域', trigger: 'blur'}
                ]
            },
            addVisible: false, // 新增弹窗的显示和隐藏
            isRead: false, // 新增、编辑或查看时，数据是否可以修改
            areaList: [], // 区域列表
            formData: {
                name: '',
                bulidName: '',
                setAmount: 0,
                description: '',
                remark: '',
                areaId: ''
            }, // 新增/编辑表单数据
            queryData: {
                name: '',
                areaId: ''
            }, // 查询表单数据
            entityData: {}, // 查看详情的数据
            pageSize: 5, // 分页数据
            pageIndex: 1, // 分页数据
            total: 0,
            baseuser: JSON.parse(Cookies.get('baseUser'))
        };
    },
    mounted () {
        this.init();
    },
    methods: {
        init: function () { // 页面初始化
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData(
                {CODE: 'AREA_TYPE'}
            )).then(res => { // 获得区域数据
                if (res.data.code == 200) {
                    var data = res.data.data;
                    this.areaList = data;
                }
            });
            this.doQuery();
        },
        doQuery: function () {
            util.ajax.post('/base/classroom/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                name: this.queryData.name,
                areaId: this.queryData.areaId
            })).then(res => {
                if (res.data.data) {
                    this.dataList = res.data.data.list;
                    this.total = res.data.data.total;
                }
            });
        },
        showAdd: function (obj) {
            let loginUser = Cookies.get('baseUser');
            if (typeof (loginUser) === 'string') {
                loginUser = JSON.parse(loginUser);
            }
            this.formData = {
                name: '',
                bulidName: '',
                setAmount: 0,
                description: '',
                remark: '',
                areaId: loginUser.areaId
            };
            this.addVisible = true;
            // 查询部门
            util.ajax.post('base/organize/getDepByOrgIdExceptOrg', util.toFormData({
                id: this.baseuser.orgId
            })).then(res => {
                this.depList = res.data.data;
                if (data.departmentId != null && data.departmentId != '') {
                    data.departmentId = parseInt(data.departmentId);
                }
            });
        },
        doDel: function (id) {
            if (id) {
                this.$Modal.confirm({
                    title: '提示',
                    content: '确认删除？',
                    onOk: () => {
                        util.ajax.post('/base/classroom/delete', util.toFormData({id: id})).then(res => {
                            if (res.data.code == 200) {
                                if (res.data.message == 'SUCCESS') {
                                    this.$Message.success('删除成功！');
                                    this.pageIndex = 1;
                                    this.init();
                                } else {
                                    this.$Message.warning(res.data.message || '未知错误！');
                                }
                            }
                        });
                    },
                    onCancel: () => {

                    }
                });
            } else {
                this.$Modal.error({content: '请选择删除项!'});
            }
        },
        doSave: function (name) {
            let formData = this.formData;
            this.$refs[name].validate((valid) => { // 表单验证
                if (valid) {
                    util.ajax.post('/base/classroom/save', util.toFormData(formData)).then(res => {
                        if (res.data.code == 200) {
                            if (res.data.message == 'SUCCESS') {
                                this.$Message.success('保存成功！');
                                this.addVisible = false;
                                this.pageIndex = 1;
                                this.init();
                            } else {
                                this.$Message.warning(res.data.message || '未知错误！');
                                this.addVisible = false;
                            }
                            this.formData = {};
                        }
                    });
                } else {
                    this.$Message.warning('请检查输入项！');
                }
            });
        },
        showEdit (obj) {
            this.formData = {};
            util.ajax.post('/base/classroom/detail', util.toFormData({
                id: obj.id
            })).then(res => {
                if (res.data.data) {
                    this.formData = res.data.data;
                    this.addVisible = true;
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        whenSelectRow: function (currentRow, oldCurrentRow) {
            var id = currentRow.id;
            this.entityData.id = id;
        }
    }
};
